/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
var serviceURL = "http://localhost:81/tauschboerse-server/";
var ID;
var articleResult = [];
var passDataObject = { selectedId: null };
var username, email,  pwd, passedId, donnee;
var localStorage = {
    firstname: null,
    lastname: null,
    username: null,
    email: null,
    image: null,
    name:null
};

$(document).on('pagebeforeshow','#index-page',function () {
    $('#login_id').on("click",function () {
        email = $('#email').val();
        pwd = $('#password').val();
        if (email.trim() === "" || pwd.trim() === "") {
            $('#errorlogin').html('les cases doivent être remplies').
                    css('color','red');
        } else {
            connexion();
        }
    });
    
    function connexion (){
        $.post("http://localhost:81/tauschboerse-server/index.php/login", {email: email, password: pwd})
            .done(function (data) {
//                        alert( "Data Loaded: " + data.message );
                if (data.error === false) {
                        if (typeof (Storage) !== "undefined") {
                            localStorage.username = data.username;
                        }
                    $.mobile.changePage("partials/articleList.html", 
                    {transition: "slideup",
                        data: {
                            email: this.email,
                            username: data.username
                        }});
                } else {
                    $('#errorlogin').html(data.message).css('color','red');
                }
            })
            .fail(function (data) {
                alert(data.message);
            });
    };

});

$(document).on('pagebeforeshow', '#article-page', function () {

//    $.get("http://localhost:81/tauschboerse-server/index.php/articles")
  $.getJSON(serviceURL+"articles")   
            .done(function (data) {
                articleResult = data;
//                $('#liste-djoka li').remove();
                $.each(data, function (key, value) {
//                $("#liste-djoka").listview({
//                    defaults: true
//                });
//                    $('#liste-article').append('<li><a href="./articleDetails.html?id=' + value.article_Id + '" id="' + value.article_Id + '" name="' + value.name + '">' +
                    $('#liste-article').append('<li rel="'+ value.article_Id +'" id="'+value.article_Id+'"><a href="#' + value.article_Id + '" id="' + value.article_Id + '" name="' + value.name + '">' +
//                            '<img src="http://localhost:81/tauschboerse-server/' + value.image + '"/>' +
                    '<img src="'+ serviceURL + value.image +'"/>' +
                            '<h4>' + value.name + ' </h4> </a></li>');

                    console.log(key + ' ' + value.article_Id + ' ' + value.name);
                });
                
                $('#liste-article').listview('refresh');
                $('#page').page('refresh', true);
                
                
    });
    
    
        
});

$(document).on("click", "#liste-article li", function () {
        ID = parseInt(($(this).attr('rel')));

        $.mobile.changePage('articleDetails.html');   // change to it 


        $('#details-page').on('pageshow', function (event) {

            $.getJSON(serviceURL + "article/" + ID)
                    .done(function (data) {
                        displayArticle(data);
                    });
        });
    });
function displayArticle(data) {
    var article = data;
	console.log(article);
        
	$("#articlePic").attr("src", serviceURL + article.image);
	$("#articleName").text(article.name);
        $("#article_Id").text(article.article_Id);
        $("#UserName").text(article.name);
        
        $("#page").page("refresh", true);
}

function getUrlVars() {
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}

$(document).on('pagebeforeshow', '[data-role="page"]', function(){
//    var username = data.username;
    $('#mypanel').appendTo($(this));
    $('#hellotag').html("Hello " + localStorage.username);
    
    $.mobile.activePage.find('#mypanel').panel();
    $(document).on('click', '#open-panel', function(){   
         $.mobile.activePage.find('#mypanel').panel("open");       
    });    
});

 